import 'package:local_auth/local_auth.dart';
import 'package:local_auth_android/local_auth_android.dart';
// import 'package:local_auth_ios/local_auth_ios.dart';

class LocalAuthUtil {
  ///单例
  LocalAuthUtil._();

  static LocalAuthUtil? _instance;

  static LocalAuthUtil get instance => _getInstance();

  factory LocalAuthUtil() => _getInstance();

  static LocalAuthUtil _getInstance() {
    _instance ??= LocalAuthUtil._();
    return _instance!;
  }

  /// 本地认证框架
  final LocalAuthentication localAuth = LocalAuthentication();

  ///自定义认证信息
  // final IOSAuthMessages _iosAuthMessages = const IOSAuthMessages(
  //   lockOut: '尝试次数超过限制，请稍后再试',
  //   goToSettingsButton: '前往设置',
  //   goToSettingsDescription: '请前往设备设置中启用身份验证',
  //   cancelButton: '取消',
  //   localizedFallbackTitle: '使用密码验证',
  // );
  final AndroidAuthMessages _androidAuthMessages = const AndroidAuthMessages(
    biometricHint: '请验证您的指纹',
    biometricNotRecognized: '指纹不匹配，请重试',
    biometricRequiredTitle: '使用指纹验证',
    biometricSuccess: '指纹验证成功',
    cancelButton: '取消',
    deviceCredentialsRequiredTitle: '设备凭据',
    deviceCredentialsSetupDescription: '请验证您的设备凭据',
    goToSettingsButton: '前往设置',
    goToSettingsDescription: '无法使用指纹验证，请在设置中激活',
    signInTitle: '验证信息',
  );

  Future<bool> onUnlock() async {
    try {
      bool didAuthenticate = await localAuth.authenticate(
          localizedReason: '进行身份识别',
          authMessages: [_androidAuthMessages]);
      return didAuthenticate;
    } catch (e) {
      print(e.toString());
    }
    return false;
  }

  Future<List<BiometricType>> findLockList() async {
    try {
      List<BiometricType> availableBiometrics =
          await localAuth.getAvailableBiometrics();
      print(availableBiometrics);
      return availableBiometrics;
    } catch (e) {
      print(e);
    }
    return [];
  }

  Future<bool> hasSysLock() async {
    try {
      bool canCheckBiometrics = await localAuth.canCheckBiometrics;
      print(canCheckBiometrics);
      return canCheckBiometrics;
    } catch (e) {
      print(e);
    }
    return false;
  }
}
